package LeetCode;

import java.util.HashMap;
import java.util.Map;

//
// Created by ✎﹏ℳ๓独赏☆独醉 on 2024/6/19.
//
//https://leetcode.cn/problems/contains-duplicate-ii/
public class 存在重复元素II {
    public static void main(String[] args) {

    }
    public static boolean containsNearbyDuplicate(int[] nums, int k) {
        Map<Integer,Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            if(map.containsKey(nums[i]) && i - map.get(nums[i]) <= k){
                return true;
            }
            map.put(nums[i],i);
        }
        return false;
    }
}
